from django import template
from django.conf import settings

register = template.Library()

def ga():
    urchin_id = getattr(settings, 'URCHIN_ID', None)
    if urchin_id:
        return """<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%%3E%%3C/script%%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("%s");
pageTracker._trackPageview();
} catch(err) {}</script>""" % settings.URCHIN_ID
    else:
        return ""

register.simple_tag(ga)
